package ${package}.controller;

import ${package}.domain.${className};
import ${package}.service.I${className}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import ${package}.common.response.BaseResponse;
import ${package}.common.query.${className}Query;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import ${package}.common.exception.BadRequestException;

import org.springframework.data.domain.Page;

/**
* @author ${author}
* @date ${date}
*/
@Controller
public class ${className}Controller {

    @Autowired
    private I${className}Service ${changeClassName}Service;


    private static final String ENTITY_NAME = "${changeClassName}";

    /**
    * 返回页面
    *
    */
    @GetMapping(value = "/${changeClassName}s")
    public String get${className}Index(){
        return "/${changeClassName}/${changeClassName}";
    }

    /**
    * 按ID查询
    *
    */
    @GetMapping(value = "/api/${changeClassName}/{id}")
    public ResponseEntity<BaseResponse<${className}>> get${className}(@PathVariable ${pkColumnType} id){
        BaseResponse<${className}> baseResponse = new BaseResponse<>();
        ${className} ${changeClassName}  = ${changeClassName}Service.findById(id);
        baseResponse.setCode(200);
        baseResponse.setMessage("success");
        baseResponse.setData(${changeClassName});
        return new ResponseEntity<>(baseResponse, HttpStatus.OK);
    }

    /**
    * 按查询条件查询
    *
    */
    @PostMapping(value = "/api/${changeClassName}s")
    public ResponseEntity<BaseResponse<Page<${className}>>> get${className}s(@RequestBody ${className}Query query){
        PageRequest pageRequest = PageRequest.of(query.getPage(), query.getSize(), new Sort(Sort.Direction.fromString(query.getOrder()), query.getSort()));
        BaseResponse<Page<${className}>> baseResponse = new BaseResponse<>();
        Page<${className}> all = ${changeClassName}Service.getAll(query, pageRequest);
        baseResponse.setCode(200);
        baseResponse.setMessage("success");
        baseResponse.setData(all);
        return new ResponseEntity<>(baseResponse, HttpStatus.OK);
    }

    /**
    * 创建
    *
    */
    @PostMapping(value = "/api/${changeClassName}")
    public  ResponseEntity<BaseResponse<Page<${className}>>> create(@Validated @RequestBody ${className} resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setMessage("success");
        baseResponse.setCode(200);
        baseResponse.setData(${changeClassName}Service.create(resources));
        return new ResponseEntity<>(baseResponse,HttpStatus.CREATED);
    }

    /**
    * 修改
    *
    */
    @PutMapping(value = "/api/${changeClassName}")
    public ResponseEntity update(@Validated @RequestBody ${className} resources){
        if (resources.getId() == null) {
            throw new BadRequestException(ENTITY_NAME +" ID Can not be empty");
        }
        ${changeClassName}Service.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    /**
    * 删除
    *
    */
    @DeleteMapping(value = "/api/${changeClassName}s/{id}")
    public ResponseEntity delete(@PathVariable Long id){
        ${changeClassName}Service.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}